import DB from "@/common/sqlite.js";

//  数据查询
let sqlStr = 'SELECT *,rowid "NAVICAT_ROWID" FROM "main"."home_drink_info" LIMIT 0,1000'

export default {

	initLocalDataBase() {

		var sqlOpenState = DB.isOpen();
		// console.log(sqlOpenState);
		if (!sqlOpenState || typeof(sqlOpenState) == undefined) {
			// 创建/开启数据库
			DB.openSqlite()
			// console.log("数据库初始化完成1")
		} else {
			// console.log("数据库初始化完成2")
		}

	},

	getDBOpenState() {

		var sqlOpenState = DB.isOpen();
		return sqlOpenState;

	},


	// -------------------------------------------------------首场
	// 收藏小说信息
	async getInforAboutBooksInYourCollection() {
		var that = this;
		this.selectCollectionTableData((type, res) => {
			if (type == "error") {
				that.initCollectionTable();
			}
		})
	},

	async initCollectionTable() {
		var isOpen = this.getDBOpenState();
		if (isOpen) {

			let sql =
				'"id" INTEGER PRIMARY KEY AUTOINCREMENT,"author" text,"authorid" integer,"bookcode" integer,"catecode" integer,"cateName" text,"bookinfo" text';
			var res = await DB.createTable("user_collection_info", sql);
			return res
		}
	},

	async selectCollectionTableData(callBack) {
		let open = DB.isOpen();
		if (open) {
			// 查询表 DB.selectTableData(表名,查询条件列名,查询条件列值)
			DB.selectTableData("user_collection_info")
				.then(res => {
					callBack("success", res)
				})
				.catch(error => {

					if (error.message.indexOf("no such table: user_collection_info") != -1) {
						callBack("error", error)
					} else {
						callBack("error2", error)
					}

				});
		} else {
			return null
			// this.showToast("数据库未打开");
		}
	},

	async selectCollectionTableData2() {
		var that = this;
		let open = DB.isOpen();
		if (open) {

			try {
				var res = await DB.selectTableData("user_collection_info")
				return res;
			} catch (e) {
				if (e.message.indexOf("no such table: user_collection_info") != -1) {
					that.initCollectionTable();
					return [];
				}

			} finally {
				// console.log('finally');
			}
			// 查询表 DB.selectTableData(表名,查询条件列名,查询条件列值)
		} else {
			return null
			// this.showToast("数据库未打开");
		}
	},




	//  插入收藏数据
	// 新增表数据
	async insertCollectionTableTableData(params) {

		var that = this;
		this.selectCollectionTableData((type, res) => {
			var oldDatas = res;
			var newInserDatas = [];
			params.forEach(function(item, index) {
				let isSaveState = false;
				oldDatas.forEach(function(item1, index1) {
					if (item1["bookcode"] == item["bookCode"]) {
						isSaveState = true
					}
				})
				if (isSaveState == false) {
					var inserItem = {}
					inserItem["author"] = item.author;
					inserItem["authorid"] = item.authorId;
					inserItem["bookcode"] = item.bookCode;
					inserItem["catecode"] = item.catecode;
					inserItem["catename"] = item.cateName;
					inserItem["bookinfo"] = JSON.stringify(item)
					newInserDatas.push(inserItem);
				}
			})
			newInserDatas.map(item => {
				let sql =
					`'${item.author}','${item.authorid}','${item.bookcode}','${item.catecode}','${item.catename}','${item.bookinfo}'`;
				let condition = "'author','authorid','bookcode','catecode','catename','bookinfo'";
				// 新增 DB.insertTableData(表名, 对应表头列的数据)
				DB.insertTableData("user_collection_info", sql, condition)
					.then(res => {
						// this.showToast("新增数据成功");
						console.log("新增数据成功", res);
					})
					.catch(error => {
						console.log("失败", JSON.stringify(error));
					});
			})
		});
	},

	async deleteCollectionTableTableData(params,callBack) {
		let open = DB.isOpen();
		if (open) {
			// 删除表 DB.deleteTableData(表名,查询条件列名,查询条件列值)
			DB.deleteTableData("user_collection_info", "bookCode", params.bookCode)
				.then(res => {
					callBack("success", res)
				})
				.catch(error => {
					callBack("error", error)
				});
		} else {
			callBack("noOpen", "")
		}
	},


	//  插入阅读数据----------------------------------------------------------------------------------
	// 收藏小说信息
	async getInforAboutBooksInYourRead() {
		var that = this;
		this.selectReadTableData((type, res) => {
			if (type == "error") {
				that.initReadBookTable();
			}
		})
	},

	async initReadBookTable() {
		var isOpen = this.getDBOpenState();
		if (isOpen) {
			let sql =
				'"id" INTEGER PRIMARY KEY AUTOINCREMENT,"author" text,"authorid" integer,"bookcode" integer,"catecode" integer,"cateName" text,"bookinfo" text';
			var res = await DB.createTable("user_read_info", sql);
			return res
		}
	},


	// 新增表数据
	async insertBookReadData(params) {


		var that = this;
		this.selectReadTableData((type, res) => {
			var oldDatas = res;
			var newInserDatas = [];
			params.forEach(function(item, index) {
				let isSaveState = false;
				oldDatas.forEach(function(item1, index1) {
					if (item1["bookcode"] == item["bookCode"]) {
						isSaveState = true
					}
				})
				if (isSaveState == false) {
					var inserItem = {

					}
					inserItem["author"] = item.author;
					inserItem["authorid"] = item.authorId;
					inserItem["bookcode"] = item.bookCode;
					inserItem["catecode"] = item.catecode;
					inserItem["catename"] = item.cateName;
					inserItem["bookinfo"] = JSON.stringify(item)
					newInserDatas.push(inserItem);
				}

			})
			newInserDatas.map(item => {
				let sql =
					`'${item.author}','${item.authorid}','${item.bookcode}','${item.catecode}','${item.catename}','${item.bookinfo}'`;
				let condition = "'author','authorid','bookcode','catecode','catename','bookinfo'";
				// 新增 DB.insertTableData(表名, 对应表头列的数据)
				DB.insertTableData("user_read_info", sql, condition)
					.then(res => {
						console.log("新增数据成功", res);
					})
					.catch(error => {
						console.log("失败", JSON.stringify(error));
					});
			})
		});


	},

	async selectReadTableData2() {
		var that = this;
		let open = DB.isOpen();
		if (open) {

			try {
				var res = await DB.selectTableData("user_read_info")
				return res;
			} catch (e) {
				if (e.message.indexOf("no such table: user_read_info") != -1) {
					that.initCollectionTable();
					return [];
				}

			} finally {
				// console.log('finally');
			}
			// 查询表 DB.selectTableData(表名,查询条件列名,查询条件列值)
		} else {
			return null
			// this.showToast("数据库未打开");
		}
	},

	async selectReadTableData(callBack) {
		let open = DB.isOpen();
		if (open) {
			// 查询表 DB.selectTableData(表名,查询条件列名,查询条件列值)
			DB.selectTableData("user_read_info")
				.then(res => {
					callBack("success", res)
				})
				.catch(error => {

					if (error.message.indexOf("no such table: user_read_info") != -1) {
						callBack("error", error)
					} else {
						callBack("error2", error)
					}

				});
		} else {
			return null
		}
	},









}